﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using UtilZ.Dotnet.Ex.Model;

namespace DFWpfApp1
{
    /// <summary>
    /// TestTreeDataGrid.xaml 的交互逻辑
    /// </summary>
    public partial class TestTreeDataGrid : Window
    {
        public TestTreeDataGrid()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            //TreeGrid.ItemsSource
        }
    }



    internal class TestTreeDataGridVM : NotifyPropertyChangedAbs
    {
        private ObservableCollection<TreeDataGridNode> _nodes = null;
        /// <summary>
        /// 子节点集合
        /// </summary>
        public ObservableCollection<TreeDataGridNode> Nodes
        {
            get { return _nodes; }
            set
            {
                _nodes = value;
                base.OnRaisePropertyChanged(nameof(Nodes));
            }
        }


        public TestTreeDataGridVM()
        {
            var nodes = new ObservableCollection<TreeDataGridNode>();
            int min = 1, max = 100;
            Random rnd = new Random();

            TreeDataGridNode node1, node2, node3, node4;
            int rootCount = 3, twoCount, threeCount, four;
            int offset0 = 5, offset1 = 15, offset2 = 25, offset3 = 35;

            for (int i = 1; i < rootCount; i++)
            {
                twoCount = 3;// rnd.Next(2, 5);
                node1 = new TreeDataGridNode(null, offset0, $"{i}-研电侦{twoCount}", $"XXX{rnd.Next(min, max)}", $"YYY{rnd.Next(min, max)}");
                nodes.Add(node1);

                for (int j = 1; j < twoCount; j++)
                {
                    threeCount = 2;// rnd.Next(2, 5);
                    node2 = new TreeDataGridNode(node1, offset1, $"{i}.{j}-研电侦{threeCount}", $"XXX{rnd.Next(min, max)}", $"YYY{rnd.Next(min, max)}");
                    //node2.AllowEdit = true;
                    nodes.Add(node2);


                    for (int k = 1; k < threeCount; k++)
                    {
                        node3 = new TreeDataGridNode(node2, offset2, $"{i}.{j}.{k}-研电侦", $"XXX{rnd.Next(min, max)}", $"YYY{rnd.Next(min, max)}");
                        nodes.Add(node3);

                        four = 4;
                        for (int m = 1; m < four; m++)
                        {
                            node4 = new TreeDataGridNode(node3, offset3, $"{i}.{j}.{k}.{m}-研电侦", $"XXX{rnd.Next(min, max)}", $"YYY{rnd.Next(min, max)}");
                            //node4.AllowEdit = true;
                            nodes.Add(node4);
                        }
                    }
                }
            }

            this._nodes = nodes;
        }
    }

    public class TreeDataGridNode : TreeDataGridNodeAbs
    {
        public override string Text
        {
            get { return base.Text; }
            set
            {
                base.Text = value;
                this.Token = value;
            }
        }

        private string _token = null;
        public string Token
        {
            get { return _token; }
            set
            {
                _token = value;
                base.OnRaisePropertyChanged(nameof(Token));
            }
        }

        private string _project;
        public string Project
        {
            get { return _project; }
            set
            {
                _project = value;
                base.OnRaisePropertyChanged(nameof(Project));
            }
        }

        public TreeDataGridNode(TreeDataGridNode parent, double nodeLocationOffset, string text, string token, string project)
            : base(text, nodeLocationOffset, parent)
        {
            this._token = token;
            this._project = project;
        }
    }
}
